package com.carlinfo.stu_ques.client;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.carlinfo.stu_ques.common.util.DateUtil;
import com.carlinfo.stu_ques.question.pojo.Answer;
import com.carlinfo.stu_ques.question.pojo.Question;
import com.carlinfo.stu_ques.question.service.IDemoService;
import com.carlinfo.stu_ques.question.service.impl.DemoServiceImpl;

public class Client
{
	private	DateUtil dateUtil = new DateUtil();
	private IDemoService demoService = new DemoServiceImpl();
	public static void main(String[] args)
	{
		Client cma = new Client();
		cma.lanuch();
	}

	private void lanuch()
	{
		System.out.println("欢迎来到本程序,按h可以查看帮助");
		InputStream in = System.in;
		BufferedReader br = null;
		try
		{
			InputStreamReader isr = new InputStreamReader(in,"UTF-8");
			br = new BufferedReader(isr);
			String line = "";
			while(true)
			{
				System.out.println("主菜单，按下h可以查看帮助，按n可以退出程序");
				line = br.readLine();
				line = line.trim();
				if ("h".equalsIgnoreCase(line))
				{
					help();
				}else if ("n".equalsIgnoreCase(line))
				{
					System.out.println("欢迎下次再来");
					break;
				}else if ("1".equalsIgnoreCase(line))
				{
					this.selectListAnswer();
				}else if ("2".equalsIgnoreCase(line))
				{
					this.saveOneAnswer(br);
				}else if ("5".equalsIgnoreCase(line))
				{
					this.selectOneAnswer(br);
				}else if ("3".equalsIgnoreCase(line))
				{
					this.updateOneAnswer(br);
				}else if ("4".equalsIgnoreCase(line))
				{
					this.deleteOneAnswer(br);
				}else if ("6".equalsIgnoreCase(line))
				{
					this.selectListQuestion();
				}else if ("7".equalsIgnoreCase(line))
				{
					this.saveOneQuestion(br);
				}else if ("10".equalsIgnoreCase(line))
				{
					this.selectOneQuestion(br);
				}else if ("8".equalsIgnoreCase(line))
				{
					this.updateOneQuestion(br);
				}else if ("9".equalsIgnoreCase(line))
				{
					this.deleteOneQuestion(br);
				}
				
			}
		} catch (IOException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}catch (Exception e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally
		{
			try
			{
				if (br != null)
				{
					br.close();
					br = null;
				}
			} catch (IOException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	private void deleteOneQuestion(BufferedReader br)
	{
		System.out.println("请输入要删除的id");
		Question question = this.selectOneQuestion(br);
		this.demoService.deleteOneQuestionService(question);
		System.out.println("删除成功");		
	}

	private void updateOneQuestion(BufferedReader br)
	{
		Question question = this.selectOneQuestion(br);
		if (question == null)
		{
			return;
		}
		System.out.println("更新后请按照示例输入，示例："+question.getName()+"|"+question.getScore()+"|"+question.getPeopleNum());
		try
		{
			String line = br.readLine();
			line = line.trim();
			String [] lines = line.split("\\|");
			if (lines.length!=3)
			{
				System.out.println("输入有误");
				return;
			}
			question.setName(lines[0]);
			question.setScore(Integer.valueOf(lines[1]));
			question.setPeopleNum(Integer.valueOf(lines[2]));
			question.setCreatTime(new Date());
			question.setEndTime(new Date());
			this.demoService.updateOneQuestionService(question);
			System.out.println("更新成功，id："+question.getId());
		} catch (NumberFormatException e)
		{	
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
	}

	private Question selectOneQuestion(BufferedReader br)
	{
		System.out.println("请输入要查询的问题的编号");
		Question question = new Question();
		try
		{
			String line = br.readLine();
			line = line.trim();
			Map<String,Object> condMap = new HashMap<String,Object>();
			condMap.put("id", line);
			question   =  this.demoService.selectOneQuestionService(condMap);
			if (question == null)
			{
				System.out.println("搜索不到此问题");
			}else 
			{
				String title = this.demoService.printTitleAnswerService();
				System.out.println(title);
				System.out.println(question);
			}
			return question;
		} catch (NumberFormatException e)
		{
			// TODO Auto-generated catch block
			//e.printStackTrace();
			System.out.println("输入的id应该为整数");
		}catch (IOException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	private void saveOneQuestion(BufferedReader br)
	{
		System.out.println("请输入要保存的问题，示例：金|100|13");
		try
		{
			String line = br.readLine();
			line = line.trim();
			String [] lines = line.split("\\|");
			if (lines.length!=3)
			{
				System.out.println("输入有误");
				return;
			}
			Question question = new Question();
			question.setName(lines[0]);
			question.setScore(Integer.valueOf(lines[1]));
			question.setPeopleNum(Integer.valueOf(lines[2]));
			question.setCreatTime(new Date());
			question.setEndTime(new Date());
			this.demoService.saveOneQuestionService(question);
			System.out.println("保存成功，id："+question.getId());
		} catch (NumberFormatException e)
		{
			// TODO Auto-generated catch block
			//e.printStackTrace();
			System.out.println("输入有误");
		} catch (IOException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
	}

	private void selectListQuestion()
	{
		List<Question> questionlist = new ArrayList<Question>();
		questionlist = this.demoService.selectListQuestionService();
		String title = this.demoService.printTitleQuestionService();
		System.out.println(title);
		for (Question question : questionlist)
		{
			System.out.println(question);
		}
	}

	private void deleteOneAnswer(BufferedReader br)
	{
		System.out.println("请输入要删除的id");
		Answer an = this.selectOneAnswer(br);
		if (an == null)
		{
			System.out.println("你要删除的内容不存在");
		}else 
		{
			this.demoService.deleteOneAnswerService(an);
			System.out.println("删除成功");
		}
		
		
	}

	private void updateOneAnswer(BufferedReader br)
	{

		Answer an = this.selectOneAnswer(br);
		if (an == null)
		{
			return;
		}
		System.out.println("更新后请按照示例输入，示例："+an.getSdId()+"|"+an.getAnswer()+"|"+an.getFolg());
		try
		{
			String line = br.readLine();
			line = line.trim();
			String [] lines = line.split("\\|");
			an.setSdId(Integer.valueOf(lines[0]));
			an.setAnswer(lines[1]);
			an.setFolg(lines[2]);
			an.setEndTime(new Date());
			this.demoService.updateOneAnswerService(an);
			System.out.println("更新成功，id："+an.getId());
		} catch (NumberFormatException e)
		{
			e.printStackTrace();
		} catch (IOException e)
		{
			e.printStackTrace();
		}
	}

	private Answer selectOneAnswer(BufferedReader br)
	{
		System.out.println("=查询单个答案，请输入id=");
		Answer an = new Answer();
		try
		{
			String line = br.readLine();
			line = line.trim();
			Map<String,Object> condMap = new HashMap<String,Object>();
			condMap.put("id", line);
			an = this.demoService.selectOneAnswerService(condMap);
			if (an == null)
			{
				System.out.println("搜索不到此答案");
			}else 
			{
				String title = this.demoService.printTitleAnswerService();
				System.out.println(title);
				System.out.println(an);
			}
			return an;
		} catch (NumberFormatException e)
		{
			//e.printStackTrace();
			System.out.println("输入的id应该为整数");
		} catch (IOException e)
		{
			e.printStackTrace();
		}
		return null;
	}

	private void saveOneAnswer(BufferedReader  br)
	{
		System.out.println("请输入要保存的答案，示例：3|字节流|true");
		try
		{
			String line = br.readLine();
			line = line.trim();
			String [] lines = line.split("\\|");
			if (lines.length!=3)
			{
				System.out.println("输入有误");
				return;
			}
			Answer an = new Answer();
			an.setSdId(Integer.valueOf(lines[0]));
			an.setAnswer(lines[1]);
			an.setFolg(lines[2]);
			an.setCreateTime(new Date());
			an.setEndTime(new Date());
			this.demoService.saveOneAnswerService(an);
			System.out.println("保存成功，id："+an.getId());
		} catch (NumberFormatException e)
		{
			//e.printStackTrace();
			System.out.println("你输入的编号不正确");
		} catch (IOException e)
		{
			e.printStackTrace();
		}catch (Exception e)
		{
			e.printStackTrace();
		}
	}

	private void selectListAnswer()
	{
		List<Answer> an = this.demoService.selectListAnswerService(); 
		String title = this.demoService.printTitleAnswerService();
		System.out.println(title);
		for (Answer answer : an)
		{
			System.out.println(answer.getId()+"\t"+answer.getSdId()+"\t"+answer.getAnswer()+"\t"+answer.getFolg()
			+"\t\t\t"+dateUtil.dateTimeToStr(answer.getCreateTime())+"\t"+dateUtil.dateTimeToStr(answer.getEndTime()));
		}
	}
	
	private void help()
	{
		System.out.println("1:查询所有答案");
		System.out.println("2:添加答案");
		System.out.println("3:修改答案;(先输入答案id,填写要修改的内容)");
		System.out.println("4:删除答案");
		System.out.println("5:查看单个答案");
		System.out.println("6:查询所有问题");
		System.out.println("7:添加问题");
		System.out.println("8:修改问题;(先输入问题id,填写要修改的内容)");
		System.out.println("9:删除问题");
		System.out.println("10:查看单个问题");
	}
}
